<?php 

Class voteModel extends appModel
{
	
	public function ad_list($page, $limit = 10)
	{
		$listPage = array('data' => '', 'pageLink' => '');
		$result = $this->show_page("SELECT * FROM ".$this->table('vote')." ORDER BY id DESC", $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		if($listPage['data'])
		foreach ($listPage['data'] as $k => $v)
		{
			$listPage['data'][$k]['addtime'] = date("Y-m-d",$v['addtime']);
			$listPage['data'][$k]['dotime1'] = date("Y-m-d",$v['dotime1']);
			$listPage['data'][$k]['dotime2'] = date("Y-m-d",$v['dotime2']);
			$listPage['data'][$k]['link'] = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?g=mobile&m=vote&a=index&id=".$v['id'];
		}
		
		return $listPage;
	}
	
	/**
	 * 获取版块内容详情
	 * @param $id
	 * @return 
	 */
	public function getVote($id)
	{
		$sql = "SELECT * FROM ".$this->table('vote')."  WHERE id = '".$id."'";
		$result = $this->getRow($sql);
		if($result)
		{
			$sql = "SELECT * FROM ".$this->table('option')."  WHERE vid = '".$id."'";
			$option = $this->getAll($sql);
			$result['option'] = $option;
			//var_dump($result);
			$result['dotime1'] = date("Y-m-d",$result['dotime1']);
			$result['dotime2'] = date("Y-m-d",$result['dotime2']);
			
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	
	/**
	 * 删除版块信息
	 * @param $id
	 * @return 
	 */
	public function del_vote($id)
	{
		$result = array('error' => 0, 'content' => '删除成功');
		if($id)
		{
			
			$sql = "DELETE FROM ".$this->table('vote')." WHERE id='".$id."'";
			$res2 = $this->query($sql);
			if(!$res || !$res2)
			{
				$result['error'] = 1;
				$result['content'] = '删除失败';
			}
		}
		return $result;
	}
	
	
	public function updateVote($info, $id)
	{
// 		if($info['status'] ==1)
// 		{
// 			$sql = "UPDATE ".$this->table('vote')." SET status =0 where 1=1 ";
// 			$this->query($sql);
// 		}
		
		if($id)
		{
			$data = yf_set_update($info);
			$sql = "UPDATE ".$this->table('vote')." SET ".$data." WHERE id='".$id."'";
			return $res = $this->query($sql);
		}
		else
		{
			
			$info['addtime'] = yf_time();
			$data = yf_set_update($info);
			
			$sql = "INSERT INTO ".$this->table('vote')." SET ".$data;
			 $this->query($sql);
			$id = $this->insert_id();
			
			return $id;
		}
	}
	
	public function updateOption($info, $id)
	{
		if($id)
		{
			
			$sql = "DELETE FROM ".$this->table('option')." WHERE vid='".$id."'";
			$res = $this->query($sql);
			
			foreach ($info as $k => $v)
			{
				if($v!="")
				{
					
					$sql = "INSERT INTO ".$this->table('option')." SET name = '".$v."', vid = ".$id;
					$res = $this->query($sql);
				}
				//$data = yf_set_insert($arr);
			
			}
			
			
			return $res;
		}
		
	}
	
}